<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\Site;
use App\Models\Template;
use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $list = Site::paginate(15);

        return response()->json(['code' => 200, 'message' => '成功', 'data' => $list]);
    }

    public function show($id)
    {
        $res = Site::find($id);
        if (!$res) {
            return response()->json(['code' => 400, 'message' => '失败']);
        } else {
            return response()->json(['code' => 200, 'message' => '成功', 'data' => $res]);
        }
    }

    public function store(Request $request)
    {
        $database = $request->post('database');
        $remark = $request->post('remark');

        $save_data = [
            'database' => $database,
            'remark' => $remark,
        ];

        $save = Site::create($save_data);
        if (!$save) {
            return response()->json(['code' => 400, 'message' => '创建站点失败']);
        } else {
            return response()->json(['code' => 200, 'message' => '创建站点成功']);
        }
    }

    public function update(Request $request, $id)
    {
        $database = $request->post('database');
        $remark = $request->post('remark');

        $save_data = [
            'database' => $database,
            'remark' => $remark,
        ];

        $save = Site::where('id', $id)->update($save_data);
        if (!$save) {
            return response()->json(['code' => 400, 'message' => '编辑站点失败']);
        } else {
            return response()->json(['code' => 200, 'message' => '编辑站点成功']);
        }
    }

    public function destroy($id)
    {
        $save = Site::where('id', $id)->delete();
        if (!$save) {
            return response()->json(['code' => 400, 'message' => '删除站点失败']);
        } else {
            return response()->json(['code' => 200, 'message' => '删除站点成功']);
        }
    }

}
